//
//  CZHomeRetweetView.swift
//  CZVVeiBo
//
//  Created by smile on 2017/8/3.
//  Copyright © 2017年 itcast. All rights reserved.
//

import UIKit
import SnapKit
class CZHomeRetweetView: UIView {
    
    // 记录约束
    var bottomConstraint: Constraint?
    
    // 提供一个属性供外界赋值
    var homeViewModel: CZHomeViewModel?{
        didSet{
            // 赋值
            contentLabel.attributedText = homeViewModel?.retweetAttributedString
            // 卸载约束
            bottomConstraint?.deactivate()
            // 判断有没有配图
            // 有配图
            if let pic_urls = homeViewModel?.homeModel?.retweeted_status?.pic_urls,pic_urls.count > 0{
                // 赋值
                pictureView.picUrls = pic_urls
                // 设置约束
                snp.makeConstraints({ (make) in
                    bottomConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
                })
                // 显示配图
                pictureView.isHidden = false
            }else{
                // 没有配图
                // 设置约束
                snp.makeConstraints({ (make) in
                    bottomConstraint = make.bottom.equalTo(contentLabel).offset(10).constraint
                })
                // 隐藏配图
                pictureView.isHidden = true

            }
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置视图
    private func setupUI(){
        backgroundColor = RGB(red: 235, green: 235, blue: 235)
        addSubview(contentLabel)
        addSubview(pictureView)
        contentLabel.snp.makeConstraints { (make) in
            make.left.top.equalTo(10)
            make.width.equalTo(CZSCREENW - 20)
        }
        
        pictureView.snp.makeConstraints { (make) in
            make.left.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp.bottom).offset(10)
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        snp.makeConstraints { (make) in
            bottomConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
        }

    }
    
    // MARK: - 懒加载控件
    // 微博文字
    private lazy var contentLabel: UILabel = {
        let lab = UILabel(textColor: UIColor.darkGray, fontSize: 14)
        lab.numberOfLines = 0
        return lab
    }()
    // 配图
    private lazy var pictureView: CZHomePictureView = {
        let view = CZHomePictureView()
        view.backgroundColor = self.backgroundColor
        return view
    }()
}

